//student grade system
#include <stdio.h>
#include <stdlib.h>
void calculateAverage(double* scores, int n, double* mid);
void findMinMax(double* scores, int n, double* min, double* max);
void sortScores(double* scores, int n);
void countGrades(double* scores, int n);

int main()
{
    //obtain the number of students
    int n;
    printf("Please input the number of students:");
    scanf(" %d", &n);
    //obtain the grades through loop variables
    double* scores = (double*)malloc(n * sizeof(double));
    for (int i = 0; i < n;)    
    {
        printf("Please input the grades %d(please input one by one):",(i + 1));
        scanf(" %lf", (scores + i));
        if (*(scores + i) >= 0)
        {
            printf("The grades %d is %.2lf\n",(i + 1),*(scores + i));
            i++;
        }
    }
    printf("--------------------\n");

    //calculate the average number
    double mid;
    calculateAverage(scores, n, &mid);
    printf("The average number of grades is %.2lf\n",mid);
    printf("--------------------\n");

    //find out the max number and the min number
    double min;
    double max;
    findMinMax(scores, n, &min, &max);
    printf("The max number is %.2lf,and the max number is %.2lf\n",max,min);
    printf("--------------------\n");

    //descending sort 
    sortScores(scores, n);
    //loop traversal
    for (int i = 0; i < n; i++)
    {
        printf("%.2lf ",*(scores + i));
    }
    printf("\n");
    printf("--------------------\n");

    //grade statistics
    countGrades(scores, n);

    free(scores);
    return 0;
    
}


//calculate the average number 
void calculateAverage(double* scores, int n, double* mid)
{
    double total = 0.0;
    for (int i = 0; i < n; i++)
    {
        total += *(scores + i);
    }  
    *mid = total / n;
}

//find out the max number and the min number
void findMinMax(double* scores, int n, double* min, double* max)
{
    *min = *scores;
    *max = *scores;
    for (int i = 0; i < n; i++)
    {
        if (*(scores + i) < *min)
        {
            *min = *(scores + i);
        }
        else if (*(scores + i) > *max)
        {
            *max = *(scores + i);   
        }
    }
}

//descending sort 
void sortScores(double* scores, int n)
{
    //inverted bubble sort
    for (int i = 1; i < n; n--)
    {
        for (int j = 0; j < (n - 1); j++)
        {
            //exchange the element
            if (*(scores + j) < *(scores + j + 1))
            {
                double temp = *(scores + j);
                *(scores + j) = *(scores + j + 1);
                *(scores + j + 1) = temp;
            }
        }
    }
}

//grade statistics
void countGrades(double* scores, int n)
{
    int count[5] = {0};
    //determine the score range
    for (int i = 0; i < n; i++)
    {
        if (*(scores + i) >= 90 && *(scores + i) <= 100)
        {
            count[0]++;
        }
        else if (*(scores + i) >= 80 && *(scores + i) < 90)
        {
            count[1]++;
        }
        else if (*(scores + i) >= 70 && *(scores + i) < 80)
        {
            count[2]++;
        }
        else if (*(scores + i) >= 60 && *(scores + i) < 70)
        {
            count[3]++;
        }
        else
        {
            count[4]++;
        }
    }
    //print the level
    printf("grade statistics:\n");
    printf("excellent(90~100):%d\n",count[0]);
    printf("good(80~89):%d\n",count[1]);
    printf("midium(70~79):%d\n",count[2]);
    printf("pass(60~69):%d\n",count[3]);
    printf("fail(0~59):%d\n",count[4]);
}